package Annotation;
import java.lang.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by studio on 2017/3/29.
 */

public class DBConnection {

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    public @interface Database{
        public String driver_class() default "com.mysql.jdbc.Driver";
        public String db_type() default "mysql";
        public String IP() default "localhost";
        public int port() default 3306;
        public String DBName();
        public String username();
        public String password();
        public boolean useSSL() default false;
    }

    public static Map getDBInfo(Class<?> clazz){
        Map arr = new HashMap();

        if(clazz.isAnnotationPresent(Database.class)){
            arr.put("driver_class",clazz.getAnnotation(Database.class).driver_class());
            arr.put("db_type",clazz.getAnnotation(Database.class).db_type());
            arr.put("IP",clazz.getAnnotation(Database.class).IP());
            arr.put("port",clazz.getAnnotation(Database.class).port());
            arr.put("DBName",clazz.getAnnotation(Database.class).DBName());
            arr.put("username",clazz.getAnnotation(Database.class).username());
            arr.put("password",clazz.getAnnotation(Database.class).password());
            arr.put("useSSL",clazz.getAnnotation(Database.class).useSSL());
        }

        return arr;
    }
}
